
****************************************************************************
******This do file replicate a forthcoming paper at Foreign Policy Analysis*
***** "Who Rallies Round the Flag? The Impact of the US Sanctions on Iranians' Attitude Toward the Government"
** Authors: RezaeeDaryakenari, Ghafouri, and Kasap *************************
****************************************************************************

***Set the woring directory accordingly 
cd "D:\Dropbox\WorkingPapers\RallyRoundFlag\DataAndCodes"

		 
************ Regression analysis: daily	

* load the data file

import delimited "Final_Replication.csv", encoding(UTF-8) clear

gen date_aux=substr(tweetcreated_at,1,10)

gen dedashed_date = subinstr(date_aux,"-","", .)

gen date = date(dedashed_date, "YMD")
format date %td

drop date_aux dedashed_date

collapse (mean) label_affiliationbased tweetretweet_count tweetfavorite_count, by(authorscreen_name affiliation nationality date)


gen date_weekly=wofd(date)
format date_weekly %tw
egen date_weekly_id=group(date_weekly)

gen date_monthly=mofd(date)
format date_monthly %tm
egen date_monthly_id=group(date_monthly)

**
replace nationality="Iranian" if nationality=="ایرانی"
replace nationality="Iranian-American" if nationality=="امریکایی - ایرانی"
**

replace affiliation="Reformist" if affiliation=="اصلاح‌طلب"
replace affiliation="Conservative" if affiliation=="اصولگرا"
replace affiliation="Overthrower" if affiliation=="برانداز"
replace affiliation="separatist" if affiliation=="تجزیه‌طلب"
replace affiliation="Transitionist" if affiliation=="تحول‌خواه"
replace affiliation="Non-Political" if affiliation=="غیرسیاسی"
replace affiliation="Unclear" if affiliation=="نامشخص"

drop if affiliation=="-"
drop if affiliation=="دولت انگلستان"

egen user_id=group(authorscreen_name)

egen affiliation_id=group(affiliation)
egen nationality_id=group(nationality)

gen IranDealWithdraw=0
replace IranDealWithdraw=1 if date>td(8may2018) & date<td(11may2018)

gen IRGC_Terrorist=0
replace IRGC_Terrorist=1 if date>td(8april2019) & date<td(11april2019)

gen EndOfIran=0
replace EndOfIran=1 if date>td(19may2019) & date<td(22may2019)

gen USDroneShotDown=0
replace USDroneShotDown=1 if date>td(20june2019) & date<td(23june2019)

gen UKIranShip=0 
replace UKIranShip=1 if date>td(4july2019) & date<td(7july2019)

gen IranUKShip=0
replace IranUKShip=1 if date>td(19july2019) & date<td(22july2019)

gen ZarifSanction=0
replace ZarifSanction=1 if date>td(31july2019) & date<td(3aug2019)

xtset user_id date, daily

eststo clear

eststo:xtreg d.label_affiliationbased l.label_affiliationbased  i.IranDealWithdraw i.IRGC_Terrorist  ///
										   i.ZarifSanction i.date_monthly_id, fe r cluster(user_id)
estadd local locationFE      "Account" , replace
estadd local timeFE          "Month" , replace
estadd local robust          "Yes" , replace
estimates store model_FET 

eststo:xtreg d.label_affiliationbased l.label_affiliationbased i.IranDealWithdraw i.IRGC_Terrorist  ///
										   i.ZarifSanction i.date_monthly_id if affiliation=="Conservative" , fe r cluster(user_id)
estadd local locationFE      "Account" , replace
estadd local timeFE          "Month" , replace
estadd local robust          "Yes" , replace
estimates store model_FET_Conservative

eststo:xtreg d.label_affiliationbased l.label_affiliationbased i.IranDealWithdraw i.IRGC_Terrorist  ///
										   i.ZarifSanction i.date_monthly_id if affiliation=="Reformist" , fe r cluster(user_id)
estadd local locationFE      "Account" , replace
estadd local timeFE          "Month" , replace
estadd local robust          "Yes" , replace
estimates store model_FET_Reformist

eststo:xtreg d.label_affiliationbased l.label_affiliationbased i.IranDealWithdraw i.IRGC_Terrorist  ///
										   i.ZarifSanction i.date_monthly_id if affiliation=="Transitionist" , fe r cluster(user_id)
estadd local locationFE      "Account" , replace
estadd local timeFE          "Month" , replace
estadd local robust          "Yes" , replace
estimates store model_FET_Transitionist
										  
eststo:xtreg d.label_affiliationbased l.label_affiliationbased i.IranDealWithdraw i.IRGC_Terrorist  ///
										   i.ZarifSanction i.date_monthly_id if affiliation=="Overthrower" , fe r cluster(user_id)
estadd local locationFE      "Account" , replace
estadd local timeFE          "Month" , replace
estadd local robust          "Yes" , replace
estimates store model_FET_Overthrower

eststo:xtreg d.label_affiliationbased l.label_affiliationbased i.IranDealWithdraw i.IRGC_Terrorist  ///
										   i.ZarifSanction i.date_monthly_id if affiliation=="Non-Political" , fe r cluster(user_id)
estadd local locationFE      "Account" , replace
estadd local timeFE          "Month" , replace
estadd local robust          "Yes" , replace
estimates store model_FET_NonPolitical

eststo:xtreg d.label_affiliationbased l.label_affiliationbased i.IranDealWithdraw i.IRGC_Terrorist  ///
										   i.ZarifSanction i.date_monthly_id if affiliation=="Unclear" , fe r cluster(user_id)
estadd local locationFE      "Account" , replace
estadd local timeFE          "Month" , replace
estadd local robust          "Yes" , replace
estimates store model_FET_Unclear

esttab using "Tables\Table_1.rtf" , replace se b(a2) star( * 0.10 ** 0.05 *** 0.010) scalars(chi2 aic bic) ///
		varwidth(25) nobaselevels interaction(" X ") onecell nogaps compress ///
		s(N r2 locationFE timeFE robust , fmt(%10.2f)) ///
		keep (_cons *.IranDealWithdraw *.IRGC_Terrorist *.ZarifSanction ) ///
		order(*.IranDealWithdraw *.IRGC_Terrorist *.ZarifSanction ) 	///
		coeflabels(_cons "Intercept"  1.IranDealWithdraw "Iran deal withdraw" 1.IRGC_Terrorist "IRGC FTO designation" )

		
esttab using "Tables\Table_1.tex" , replace se b(a2) star( * 0.10 ** 0.05 *** 0.010) scalars(chi2 aic bic) ///
		varwidth(25) nobaselevels interaction(" X ") onecell nogaps compress ///
		s(N r2 locationFE timeFE robust , fmt(%10.2f)) ///
		keep (_cons *.IranDealWithdraw *.IRGC_Terrorist *.ZarifSanction ) ///
		order(*.IranDealWithdraw *.IRGC_Terrorist *.ZarifSanction ) 	///
		coeflabels(_cons "Intercept"  1.IranDealWithdraw "Iran deal withdraw" 1.IRGC_Terrorist "IRGC FTO designation" )

		
coefplot (model_FET, msymbol(D) mlabel("All accounts") mlabposition(12) label(All accounts)) ///
		 (model_FET_Conservative, msymbol(D) mlabel("Conservatives") mlabposition(12) label(Conservatives)) ///
		 (model_FET_Reformist, msymbol(D) mlabel("Reformists") mlabposition(12) label(Reformists)) ///
		 (model_FET_Transitionist, msymbol(D) mlabel("Transitionists") mlabposition(12) label(Transitionists)) ///
         (model_FET_Overthrower, msymbol(D) mlabel("Overthrowers") mlabposition(12) label(Overthrowers)) ///
		 (model_FET_NonPolitical, msymbol(D) mlabel("Non-Political") mlabposition(12) label(Non-Political)) ///
		 (model_FET_Unclear, msymbol(D) mlabel("Unclear") mlabposition(12) label(Unclear)) ///
		 , nolabel drop(_cons *.IRGC_Terrorist  *.ZarifSanction *.label_affiliationbased *.date_monthly_id ) keep(*:) omitted ///
								scheme(lean2) xline(0) level(90) msymbol(S) ///
								rename(1.IranDealWithdraw="Iran Deal Withdraw" ///
									   1.IRGC_Terrorist="IRGC Terrorist" ///
									   1.EndOfIran="End of Iran" ///
									   1.USDroneShotDown="US Drone Shot Down" ///
									   1.UKIranShip="UK Iran ship" ///
									   1.IranUKShip="Iran UK Ship" ///
									   1.ZarifSanction="Zarif Sanction") ///
								 legend(off) ///
								ylabel(,angle(90))   ///
								title("")
gr export "Figures/coef_IranDeal.png",replace width(4000)
								
coefplot (model_FET, msymbol(D) mlabel("All accounts") mlabposition(12)  label(All accounts)) ///
		 (model_FET_Conservative, msymbol(D) mlabel("Conservatives") mlabposition(12) label(Conservatives)) ///
		 (model_FET_Reformist, msymbol(D) mlabel("Reformists") mlabposition(12) label(Reformists)) ///
		 (model_FET_Transitionist,  msymbol(D) mlabel("Transitionists") mlabposition(12) label(Transitionists)) ///
         (model_FET_Overthrower,  msymbol(D) mlabel("Overthrowers") mlabposition(12) label(Overthrowers)) ///
		 (model_FET_NonPolitical, msymbol(D) mlabel("Non-Political") mlabposition(12) label(Non-Political)) ///
		 (model_FET_Unclear, msymbol(D) mlabel("Unclear") mlabposition(12) label(Unclear)) ///
		 , nolabel drop(_cons *.IranDealWithdraw  *.ZarifSanction *.label_affiliationbased *.date_monthly_id ) keep(*:) omitted ///
								scheme(lean2) xline(0) level(90) msymbol(S) ///
								rename(1.IranDealWithdraw="Iran Deal Withdraw" ///
									   1.IRGC_Terrorist="IRGC designated as Foreign Terrorist Organization" ///
									   1.EndOfIran="End of Iran" ///
									   1.USDroneShotDown="US Drone Shot Down" ///
									   1.UKIranShip="UK Iran ship" ///
									   1.IranUKShip="Iran UK Ship" ///
									   1.ZarifSanction="Zarif Sanction") ///
								legend(off) ///
								ylabel(,angle(90))   msymbol(d) ///
								title("")
gr export "Figures/coef_IRGC.png",replace width(4000)
								
coefplot (model_FET, msymbol(D) mlabel("All accounts") mlabposition(12)  label(All accounts)) ///
		 (model_FET_Conservative, msymbol(D) mlabel("Conservatives") mlabposition(12) label(Conservatives)) ///
		 (model_FET_Reformist, msymbol(D) mlabel("Reformists") mlabposition(12) label(Reformists)) ///
		 (model_FET_Transitionist, msymbol(D) mlabel("Transitionists") mlabposition(12) label(Transitionists)) ///
         (model_FET_Overthrower, msymbol(D) mlabel("Overthrowers") mlabposition(12) label(Overthrowers)) ///
		 (model_FET_NonPolitical, msymbol(D) mlabel("Non-Political") mlabposition(12) label(Non-Political)) ///
		 (model_FET_Unclear, msymbol(D) mlabel("Unclear") mlabposition(12) label(Unclear)) ///
		 , nolabel drop(_cons *.IranDealWithdraw *.IRGC_Terrorist   *.label_affiliationbased *.date_monthly_id ) keep(*:) omitted ///
								scheme(lean2) xline(0) level(90) msymbol(S) ///
								rename(1.IranDealWithdraw="Iran Deal Withdraw" ///
									   1.IRGC_Terrorist="IRGC Terrorist" ///
									   1.EndOfIran="End of Iran" ///
									   1.USDroneShotDown="US Drone Shot Down" ///
									   1.UKIranShip="UK Iran ship" ///
									   1.IranUKShip="Iran UK Ship" ///
									   1.ZarifSanction="Zarif Sanction") ///
								legend(ring(1) position(6)  row(2)) ///
								ylabel(,angle(90))  msymbol(d) ///
								title("")
gr export "Figures/coef_Zarif.png",replace width(4000)



*******************************************************************************











								